package cn.bon.emp.mapper;


import cn.bon.emp.api.pojo.po.Org;
import cn.bon.emp.api.pojo.vo.OrgVO;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 * 组织架构 Mapper 接口
 * </p>
 *
 * @author luochao
 * @since 2023-12-09
 */
public interface OrgMapper extends BaseMapper<Org> {
    /**
     * 查询对应组织下一级的组织
     * @param parentId 父级Id
     * @return
     */
    List<OrgVO> listWithParent(Long parentId);

    /**
     * 查询单个组织放入VO
     * @param orgId 组织Id
     * @return
     */
    OrgVO getById2Vo(Long orgId);

    /**
     * 查询单个组织
     * @param orgId 组织Id
     * @return
     */
    @Select("SELECT * FROM org " +
            "WHERE org_id=#{orgId}")
    Org getById(Long orgId);

    /**
     * 插入一条org
     * @param org 插入的org
     */
    @Insert("INSERT INTO org(org_id,org_name,org_level,parent_id,org_order,create_time,update_Time,del_flag) " +
            "VALUES (#{orgId},#{orgName},#{orgLevel},#{parentId},#{orgOrder},#{createTime},#{updateTime},#{delFlag}) ")
    void save(Org org);

    /**
     * 更具ID删除
     * @param orgId 组织Id
     */
    @Delete("DELETE FROM org " +
            "WHERE org_id=#{orgId}")
    void deleteById(Long orgId);

    /**
     * 根据ID更新org
     * @param org 要更新的org
     */
    @Update("UPDATE org SET org_name=#{orgName},org_level=#{orgLevel}," +
            "parent_id=#{parentId},org_order=#{orgOrder},create_time=#{createTime}," +
            "update_Time=#{updateTime},del_flag=#{delFlag} " +
            "WHERE org_id=#{orgId}")
    void updateByID(Org org);

    /**
     * 更具父ID删除
     * @param parentId 父ID
     */
    @Update("UPDATE org SET del_flag=NULL WHERE parent_id=#{parentId}")
    void rmByParentId(Long parentId);

    /**
     * 更具自身ID删除
     * @param orgId 组织ID
     */
    @Update("UPDATE org SET del_flag=NULL WHERE org_id=#{orgId}")
    void rmById(Long orgId);
}
